<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:aOss:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:aOss:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:aOss:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aOssList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="类型" align="center" prop="currentType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.oss_type" :value="scope.row.currentType"/>
        </template>
      </el-table-column>

      <el-table-column label="appid" align="center" prop="appId" />
      <el-table-column label="appkey" align="center" prop="appKey" />
      <el-table-column label="当前使用" align="center" prop="apply">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.apply" @change="handleSwitchChange(scope.row)"  active-text="使用" inactive-text="停用" :disabled="scope.row.id === 1 && scope.row.apply"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row.id)"
            v-hasPermi="['system:aOss:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:aOss:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!--     添加或修改oss配置管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="类型" prop="currentType" >
          <el-select v-model="form.currentType" placeholder="请选择类型" :disabled="form.id === 1">
            <el-option v-for="dict in dict.type.oss_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="当前使用" prop="apply">
          <el-switch v-model="form.apply" active-text="使用" inactive-text="停用" :disabled="form.id ===1 && form.apply" />
        </el-form-item>
<!--        <div v-if="form.currentType == '2'">-->
<!--          <el-form-item label="路径" prop="ossUrl">-->
<!--            <el-input v-model="form.ossUrl" placeholder="请输入路径"  :disabled="false"/>-->
<!--          </el-form-item>-->
<!--        </div>-->
<!--        <div v-else>-->

          <el-form-item label="appid" prop="appId">
            <el-input v-model="form.appId" placeholder="请输入appid" />
          </el-form-item>
          <el-form-item label="appkey" prop="appKey">
            <el-input v-model="form.appKey" placeholder="请输入appkey" />
          </el-form-item>
        <el-form-item label="appSecret" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="请输入appSecret" />
        </el-form-item>
        <el-form-item label="bucket" prop="bucket">
          <el-input v-model="form.bucket" placeholder="请输入bucket" />
        </el-form-item>
          <el-form-item label="region" prop="region">
            <el-input v-model="form.region" placeholder="请输入region" />
          </el-form-item>
          <el-form-item label="路径" prop="url">
            <el-input v-model="form.url" placeholder="请输入路径" />
          </el-form-item>
<!--        </div>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAOss, getAOss, delAOss, addAOss, updateAOss } from "@/api/system/aOss";

export default {
  name: "AOss",
  dicts: ["oss_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // oss配置管理表格数据
      aOssList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        currentType: null,
        apply: null,
        appId: null,
        appKey: null,
        secretKey: null,
        publicKey: null,
        ossName: null,
        ossUrl: null,
        isShow: null,
        numSoft: null,
        oldUrl:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      localRules:{
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        currentType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        ossUrl: [
          { required: true, message: "路径不能为空", trigger: "blur" }
        ],
      },
      ossRules:{
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        currentType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        ossUrl: [
          { required: true, message: "路径不能为空", trigger: "blur" }
        ],
        appId: [
          { required: true, message: "appid不能为空", trigger: "blur" }
        ],
        appKey: [
          { required: true, message: "appkey不能为空", trigger: "blur" }
        ],
        ossName: [
          { required: true, message: "oss名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch:{
    'form.currentType':{
      handler(val,oldVal){
        if(val != null){
          if(val === '2'){
            this.rules = this.localRules;
          }else{
            this.rules = this.ossRules;
          }
        }
      },
      immediate:true,
      deep:true
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleSwitchChange(row){
      updateAOss(row).then(res=>{
        if(res.code == 200){
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }else{
          this.$modal.msgError(res.msg);
        }
      })
    },
    /** 查询oss配置管理列表 */
    getList() {
      this.loading = true;
      listAOss(this.queryParams).then(response => {
        this.aOssList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        currentType: null,
        apply: null,
        appId: null,
        appKey: null,
        secretKey: null,
        publicKey: null,
        ossName: null,
        ossUrl: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加oss配置管理";
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      // this.reset();
      // const id = row.id || this.ids
      getAOss(id).then(response => {
        this.form = response.data;
        this.oldUrl = response.data.ossUrl;
        this.open = true;
        this.title = "修改oss配置管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            if(this.form.id === 1 && this.form.ossUrl !== this.oldUrl){
              this.$modal.confirm('修改本地存储路径将会导致之前文件丢失，是否继续？').then(resp=>{
                if(resp === "confirm"){
                  updateAOss(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
                }
              })
            }else{
              updateAOss(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            }


          } else {
            addAOss(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除oss配置管理编号为"' + ids + '"的数据项？').then(function () {
        return delAOss(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aOss/export', {
        ...this.queryParams
      }, `aOss_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
